﻿using GKQC.Common;
using GKQC.DbHelper.DbModels;
using GKQC.DbHelper.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using static GKQC.Common.Settings;

namespace GKQC.Machine
{
    public class MachineFactory
    {
        public static object GetLcrForm(QcFormBase parentForm, UserModel user, double voltage
            , List<ElectricViewModel> electricViewModels
            , StandardView standardView, ReportSetModel report)
        {
            if(parentForm.Name == "FormFqc" || parentForm.Name == "FormOqc")
            {
                FormLCR11022_FQC f = new FormLCR11022_FQC(parentForm, user, voltage,electricViewModels, standardView, report);
                if (report != null && report.HasEsr2) f.SetEsr2Visible(true);
                if (report != null && report.HasZ2) f.SetZ2Visible(true);
                parentForm.SubFormList.Add(f);
                return f;
            }

            LcrType lcrtype = GetLcrType();
            LcType lctype = GetLcType();
            bool isAutoTest = IsAllAuto();
            if (isAutoTest)
            {
               
                SerialPort sp = new SerialPort();
                Settings.GetPortBuad(sp, MachineType.Box2);
                if (lctype == LcType.SX1601_20)
                {
                    TestParams.MaxQty = 12;
                    var f = new Form1601_20(parentForm, user, voltage, electricViewModels, standardView, report);
                    if (report != null && report.HasEsr2) f.SetEsr2Visible(true);
                    if (report != null && report.HasZ2) f.SetZ2Visible(true);
                    if (report != null && report.HasLc2) f.SetLc2Visible(true);
                    parentForm.SubFormList.Add(f);
                    return f;
                }
                else
                {
                    Form1601 f = new Form1601(parentForm, user, voltage, electricViewModels, standardView, report);
                    if (report != null && report.HasEsr2) f.SetEsr2Visible(true);
                    if (report != null && report.HasZ2) f.SetZ2Visible(true);
                    if (report != null && report.HasLc2) f.SetLc2Visible(true);
                    parentForm.SubFormList.Add(f);
                    return f;
                }
            }
            else
            {
                FormLCR11022 f = new FormLCR11022(parentForm, user, voltage, electricViewModels, standardView,report);
                if (report != null && report.HasEsr2) f.SetEsr2Visible(true);
                if (report != null && report.HasZ2) f.SetZ2Visible(true);
                parentForm.SubFormList.Add(f);
                return f;
            }
        }

        public static object GetLcForm(QcFormBase parentForm, UserModel user, double voltage
            , List<ElectricViewModel> electricViewModels
            , StandardView standardView, ReportSetModel report)
        {
            Settings.LcType lcType = Settings.GetLcType();
            if (lcType == Settings.LcType.SX650X)
            {
                Form1601 f = new Form1601(parentForm, user, voltage, electricViewModels, standardView,report);
                if (report != null && report.HasLc2) f.SetLc2Visible(true);
                f.SetLcOnly();
                parentForm.SubFormList.Add(f);
                return f;
            }
            else //(lcType == Settings.LcType.CH2311)
            {
                FormLC2311 f = new FormLC2311(parentForm, user, voltage, electricViewModels, standardView,report);
                if (report != null && report.HasLc2) f.SetLc2Visible(true);
                parentForm.SubFormList.Add(f);
                return f;
            }
        }

        public static ILcrMeter GetLcrMeter()
        {
            LcrType lcrType = Settings.GetLcrType();
            bool isAutoTest = Settings.IsAllAuto();
            ILcrMeter lcrMeter;

            if (isAutoTest)
            {
                lcrMeter = new LcrMeter19001A();
            }
            else
            {
                if (lcrType == LcrType.LCR11022)
                    lcrMeter = new LcrMeter11022();
                else if (lcrType == LcrType.CH6021)
                    lcrMeter = new LcrMeter6021();
                else if (lcrType == LcrType.SX10091A)
                    lcrMeter = new LcrMeter19001A();
                else lcrMeter = new LcrMeter11022();
            }

            return lcrMeter;
        }

        public static ILcMeter GetLcMeter()
        {
            LcType lcType = Settings.GetLcType();
            bool isAutoTest = Settings.IsAllAuto();

            ILcMeter lcMeter;
            if (isAutoTest)
            {
                if(lcType == LcType.SX1601_20)
                    lcMeter = new LcMeter1601_20();
                else lcMeter = new LcMeter1601();
            }
            else
            {
                if (lcType == LcType.CH2311)
                    lcMeter = new LcMeter2311();
                else if (lcType == LcType.SX650X)
                    lcMeter = new LcMeter1601();
                else
                    lcMeter = new LcMeter2311();
            }

            return lcMeter;
        }

        public static DataTable GetLcrFrequencyTable()
        {
            //120Hz、200Hz、400Hz、1KHz、5KHz、10KHz、50KHz、100KHz、150KHz、200KHz
            LcrType lcrType = Settings.GetLcrType();
            DataTable dtFreq = new DataTable("freqTable");
            dtFreq.Columns.Add("Value");
            dtFreq.Columns.Add("Name");
            DataRow drFreq = dtFreq.NewRow();
            string value = string.Empty;
            if (lcrType == LcrType.LCR11022)
                value = "120";
            else if (lcrType == LcrType.CH6021)
                value = "1";
            else if (lcrType == LcrType.SX10091A)
                value = "0.12KHZ";
            drFreq[0] = value;
            //drFreq[0] = _LcrType == Settings.LcrType.LCR11022 ? "120":"1";
            drFreq[1] = "120HZ";
            dtFreq.Rows.Add(drFreq);

            drFreq = dtFreq.NewRow();
            value = string.Empty;
            if (lcrType == LcrType.LCR11022)
                value = "100000";
            else if (lcrType == LcrType.CH6021)
                value = "4";
            else if (lcrType == LcrType.SX10091A)
                value = "100KHZ";
            drFreq[0] = value;
            //drFreq[0] = _LcrType == Settings.LcrType.LCR11022 ? "100000":"4";
            drFreq[1] = "100KHZ";
            dtFreq.Rows.Add(drFreq);

            drFreq = dtFreq.NewRow();
            if (lcrType == LcrType.LCR11022)
                value = "200";
            else if (lcrType == LcrType.CH6021)
                value = "8";
            else if (lcrType == LcrType.SX10091A)
                value = "0.20KHZ";
            drFreq[0] = value;
            //drFreq[0] = _LcrType == Settings.LcrType.LCR11022 ? "120":"1";
            drFreq[1] = "200HZ";
            dtFreq.Rows.Add(drFreq);

            //120Hz、200Hz、400Hz、1KHz、5KHz、10KHz、50KHz、100KHz、150KHz、200KHz
            drFreq = dtFreq.NewRow();
            if (lcrType == LcrType.LCR11022)
                value = "400";
            else if (lcrType == LcrType.CH6021)
                value = "9";
            else if (lcrType == LcrType.SX10091A)
                value = "0.40KHZ";
            drFreq[0] = value;
            //drFreq[0] = _LcrType == Settings.LcrType.LCR11022 ? "120":"1";
            drFreq[1] = "400HZ";
            dtFreq.Rows.Add(drFreq);

            drFreq = dtFreq.NewRow();
            value = string.Empty;
            if (lcrType == LcrType.LCR11022)
                value = "1000";
            else if (lcrType == LcrType.CH6021)
                value = "2";
            else if (lcrType == LcrType.SX10091A)
                value = "1KHZ";
            drFreq[0] = value;
            //drFreq[0] = _LcrType == Settings.LcrType.LCR11022 ? "1000":"2";
            drFreq[1] = "1KHZ";
            dtFreq.Rows.Add(drFreq);

            drFreq = dtFreq.NewRow();
            value = string.Empty;
            if (lcrType == LcrType.LCR11022)
                value = "5000";
            else if (lcrType == LcrType.CH6021)
                value = "12";
            else if (lcrType == LcrType.SX10091A)
                value = "5KHZ";
            drFreq[0] = value;
            //drFreq[0] = _LcrType == Settings.LcrType.LCR11022 ? "1000":"2";
            drFreq[1] = "5KHZ";
            dtFreq.Rows.Add(drFreq);

            drFreq = dtFreq.NewRow();
            value = string.Empty;
            if (lcrType == LcrType.LCR11022)
                value = "10000";
            else if (lcrType == LcrType.CH6021)
                value = "3";
            else if (lcrType == LcrType.SX10091A)
                value = "10KHZ";
            drFreq[0] = value;
            //drFreq[0] = _LcrType == Settings.LcrType.LCR11022 ? "10000" :"3";
            drFreq[1] = "10KHZ";
            dtFreq.Rows.Add(drFreq);

            drFreq = dtFreq.NewRow();
            value = string.Empty;
            if (lcrType == LcrType.LCR11022)
                value = "50000";
            else if (lcrType == LcrType.CH6021)
                value = "21";
            else if (lcrType == LcrType.SX10091A)
                value = "50KHZ";
            drFreq[0] = value;
            //drFreq[0] = _LcrType == Settings.LcrType.LCR11022 ? "1000":"2";
            drFreq[1] = "50KHZ";
            dtFreq.Rows.Add(drFreq);

            //120Hz、200Hz、400Hz、1KHz、5KHz、10KHz、50KHz、100KHz、150KHz、200KHz

            drFreq = dtFreq.NewRow();
            value = string.Empty;
            if (lcrType == LcrType.LCR11022)
                value = "150000";
            else if (lcrType == LcrType.CH6021)
                value = "27";
            else if (lcrType == LcrType.SX10091A)
                value = "150KHZ";
            drFreq[0] = value;
            //drFreq[0] = _LcrType == Settings.LcrType.LCR11022 ? "100000":"4";
            drFreq[1] = "150KHZ";
            dtFreq.Rows.Add(drFreq);

            drFreq = dtFreq.NewRow();
            value = string.Empty;
            if (lcrType == LcrType.LCR11022)
                value = "200000";
            else if (lcrType == LcrType.CH6021)
                value = "29";
            else if (lcrType == LcrType.SX10091A)
                value = "200KHZ";
            drFreq[0] = value;
            //drFreq[0] = _LcrType == Settings.LcrType.LCR11022 ? "100000":"4";
            drFreq[1] = "200KHZ";
            dtFreq.Rows.Add(drFreq);


            return dtFreq;
        }

        /// <summary>
        /// 自動測試儀頻率
        /// </summary>
        /// <returns></returns>
        public static DataTable GetLcrFrequencyTable19001()
        {
            LcrType lcrType = Settings.GetLcrType();
            DataTable dtFreq = new DataTable("freqTable");
            dtFreq.Columns.Add("Value");
            dtFreq.Columns.Add("Name");
            DataRow drFreq = dtFreq.NewRow();
            string value = "0.12KHZ";
            drFreq[0] = value;
            drFreq[1] = "120HZ";
            dtFreq.Rows.Add(drFreq);

            drFreq = dtFreq.NewRow();
            value = "100KHZ";
            drFreq[0] = value;
            drFreq[1] = "100KHZ";
            dtFreq.Rows.Add(drFreq);

            drFreq = dtFreq.NewRow();
            value = "1KHZ";
            drFreq[0] = value;
            drFreq[1] = "1KHZ";
            dtFreq.Rows.Add(drFreq);

            drFreq = dtFreq.NewRow();
            value = "10KHZ";
            drFreq[0] = value;
            drFreq[1] = "10KHZ";
            dtFreq.Rows.Add(drFreq);

            //drFreq = dtFreq.NewRow();
            //value = "0.02KHZ";
            //drFreq[0] = value;
            //drFreq[1] = "20HZ";
            //dtFreq.Rows.Add(drFreq);

            //drFreq = dtFreq.NewRow();
            //value = "0.04KHZ";
            //drFreq[0] = value;
            //drFreq[1] = "40HZ";
            //dtFreq.Rows.Add(drFreq);

            //drFreq = dtFreq.NewRow();
            //value = "0.05KHZ";
            //drFreq[0] = value;
            //drFreq[1] = "50HZ";
            //dtFreq.Rows.Add(drFreq);

            //drFreq = dtFreq.NewRow();
            //value = "0.06KHZ";
            //drFreq[0] = value;
            //drFreq[1] = "60HZ";
            //dtFreq.Rows.Add(drFreq);

            drFreq = dtFreq.NewRow();
            value = "0.1KHZ";
            drFreq[0] = value;
            drFreq[1] = "100HZ";
            dtFreq.Rows.Add(drFreq);

            drFreq = dtFreq.NewRow();
            value = "0.2KHZ";
            drFreq[0] = value;
            drFreq[1] = "200HZ";
            dtFreq.Rows.Add(drFreq);

            drFreq = dtFreq.NewRow();
            value = "0.4KHZ";
            drFreq[0] = value;
            drFreq[1] = "400HZ";
            dtFreq.Rows.Add(drFreq);

            //drFreq = dtFreq.NewRow();
            //value = "0.5KHZ";
            //drFreq[0] = value;
            //drFreq[1] = "500HZ";
            //dtFreq.Rows.Add(drFreq);

            drFreq = dtFreq.NewRow();
            value = "2KHZ";
            drFreq[0] = value;
            drFreq[1] = "2KHZ";
            dtFreq.Rows.Add(drFreq);

            drFreq = dtFreq.NewRow();
            value = "3KHZ";
            drFreq[0] = value;
            drFreq[1] = "3KHZ";
            dtFreq.Rows.Add(drFreq);

            //drFreq = dtFreq.NewRow();
            //value = "4KHZ";
            //drFreq[0] = value;
            //drFreq[1] = "4KHZ";
            //dtFreq.Rows.Add(drFreq);

            drFreq = dtFreq.NewRow();
            value = "5KHZ";
            drFreq[0] = value;
            drFreq[1] = "5KHZ";
            dtFreq.Rows.Add(drFreq);

            drFreq = dtFreq.NewRow();
            value = "10KHZ";
            drFreq[0] = value;
            drFreq[1] = "10KHZ";
            dtFreq.Rows.Add(drFreq);

            drFreq = dtFreq.NewRow();
            value = "20KHZ";
            drFreq[0] = value;
            drFreq[1] = "20KHZ";
            dtFreq.Rows.Add(drFreq);

            drFreq = dtFreq.NewRow();
            value = "30KHZ";
            drFreq[0] = value;
            drFreq[1] = "30KHZ";
            dtFreq.Rows.Add(drFreq);

            drFreq = dtFreq.NewRow();
            value = "40KHZ";
            drFreq[0] = value;
            drFreq[1] = "40KHZ";
            dtFreq.Rows.Add(drFreq);

            drFreq = dtFreq.NewRow();
            value = "50KHZ";
            drFreq[0] = value;
            drFreq[1] = "50KHZ";
            dtFreq.Rows.Add(drFreq);

            drFreq = dtFreq.NewRow();
            value = "60KHZ";
            drFreq[0] = value;
            drFreq[1] = "60KHZ";
            dtFreq.Rows.Add(drFreq);

            drFreq = dtFreq.NewRow();
            value = "70KHZ";
            drFreq[0] = value;
            drFreq[1] = "70KHZ";
            dtFreq.Rows.Add(drFreq);

            drFreq = dtFreq.NewRow();
            value = "80KHZ";
            drFreq[0] = value;
            drFreq[1] = "80KHZ";
            dtFreq.Rows.Add(drFreq);

            drFreq = dtFreq.NewRow();
            value = "90KHZ";
            drFreq[0] = value;
            drFreq[1] = "90KHZ";
            dtFreq.Rows.Add(drFreq);

            drFreq = dtFreq.NewRow();
            value = "200KHZ";
            drFreq[0] = value;
            drFreq[1] = "200KHZ";
            dtFreq.Rows.Add(drFreq);

            return dtFreq;
        }

        public static DataTable GetTestItemTable()
        {
            DataTable dt = new DataTable("newTable");
            dt.Columns.Add("Value");
            dt.Columns.Add("Name");
            DataRow dr = dt.NewRow();
            dr[0] = "CD";
            dr[1] = "CD";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "ZR";
            dr[1] = "ZR";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "Z";
            dr[1] = "Z";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "R";
            dr[1] = "R";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "CD-ZR";
            dr[1] = "CD-ZR";
            dt.Rows.Add(dr);

            return dt;
        }

        public static DataTable GetAutoTestItemTable()
        {
            DataTable dt = new DataTable("newTable");
            dt.Columns.Add("Value");
            dt.Columns.Add("Name");
            DataRow dr = dt.NewRow();
            dr[0] = "CD-ZR-LC";
            dr[1] = "CD-ZR-LC";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "CD-R-LC";
            dr[1] = "CD-R-LC";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "CD-Z-LC";
            dr[1] = "CD-Z-LC";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "CD-ZR";
            dr[1] = "CD-ZR";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "CD-LC";
            dr[1] = "CD-LC";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "ZR";
            dr[1] = "ZR";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "CD-Z";
            dr[1] = "CD-Z";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "CD-R";
            dr[1] = "CD-R";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "CD";
            dr[1] = "CD";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "LC";
            dr[1] = "LC";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "Z";
            dr[1] = "Z";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "R";
            dr[1] = "R";
            dt.Rows.Add(dr);

            return dt;
        }

        public static DataTable GetLcLevelTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Value", typeof(string)));
            dt.Columns.Add(new DataColumn("Name", typeof(string)));

            DataRow r = dt.NewRow();
            r["Value"] = "99e-6";
            r["Name"] = "99μA";
            dt.Rows.Add(r);

            r = dt.NewRow();
            r["Value"] = "999e-6";
            r["Name"] = "999μA";
            dt.Rows.Add(r);

            r = dt.NewRow();
            r["Value"] = "19e-3";
            r["Name"] = "19mA";
            dt.Rows.Add(r);

            r = dt.NewRow();
            r["Value"] = "199e-6";
            r["Name"] = "199μA";
            dt.Rows.Add(r);

            r = dt.NewRow();
            r["Value"] = "19e-6";
            r["Name"] = "19μA";
            dt.Rows.Add(r);

            r = dt.NewRow();
            r["Value"] = "9e-6";
            r["name"] = "9μA";
            dt.Rows.Add(r);

            r = dt.NewRow();
            r["Value"] = "5e-6";
            r["Name"] = "5μA";
            dt.Rows.Add(r);

            r = dt.NewRow();
            r["Value"] = "3e-6";
            r["Name"] = "3μA";
            dt.Rows.Add(r);

            r = dt.NewRow();
            r["Value"] = "1e-6";
            r["Name"] = "1μA";
            dt.Rows.Add(r);

            return dt;
        }

        public static bool ExistsTestForm(TestType testType,List<Form> forms)
        {
            bool flag = false;
            if (forms == null) return false;

            if (testType == TestType.Lcr)
            {
                foreach (Form ff in forms)
                {
                    if (ff.IsDisposed) continue;

                    Type type = ff.GetType();
                    if (type == typeof(FormLCR11022) || type == typeof(Form1601) || type == typeof(FormLCR11022_FQC))
                    {
                        ff.Activate();
                        return true;
                    }
                }
            }
            else if(testType == TestType.Lc)
            {
                foreach (Form ff in forms)
                {
                    if (ff.IsDisposed) continue;

                    Type type = ff.GetType();
                    if (type == typeof(FormLC2311) || type == typeof(Form1601))
                    {
                        ff.Activate();
                        return true;
                    }
                }
            }
            else if(testType == TestType.Size)
            {
                foreach (Form ff in forms)
                {
                    if (ff.IsDisposed) continue;

                    if (ff.GetType() == typeof(FormCaliper))
                    {
                        ff.Activate();
                        return true;
                    }
                }
            }

            return flag;
        }
    }

    public enum TestStatus
    {
        LCR參數設置,
        LCR測試中,
        LCR測試完成,
        LC參數設置,
        充電中,
        LC測試中,
        LC測試完成,
        放電中,
        停止
    }

    public enum TestType
    {
        Lcr,
        Lc,
        Size
    }
}
